"use strict";
const radon = require('radon');
const util = radon.util;
const hawkUtil = require('./util');
const validate = hawkUtil.validate;
const http_request = hawkUtil.http_request;

/*
 * 官网接口地址：https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.xbihhU&treeId=257&articleId=105735&docType=1
 */
function DingDing(url) {
	this.url = url;
}

/*
 * data参考官网对应的请求参数表格(msgtype参数不用传入)
 */
DingDing.prototype.sendText = function* (data) {
	validate(data, {
		content: String
	});

	let param = {
		msgtype: 'text',
		text: {
			content: data.content
		},
		at: {
			atMobiles: data.atMobiles || [],
			isAtAll: data.isAtAll || false
		}
	}

	yield _send(this.url, param);
}

DingDing.prototype.sendLink = function* (data) {
	validate(data, {
		title: String,
		text: String,
		messageUrl: String
	});

	let param = {
		msgtype: 'link',
		link: {
			title: data.title,
		    text: data.text,
		    messageUrl: data.messageUrl,
		    picUrl: data.picUrl || ''
		}
	}

	yield _send(this.url, param);
}

DingDing.prototype.sendMarkdown = function* (data) {
	validate(data, {
		title: String,
		text: String
	});

	let param = {
		msgtype: 'markdown',
		markdown: {
			title: data.title,
		    text: data.text
		},
		at: {
			atMobiles: data.atMobiles || [], 
			isAtAll: data.isAtAll || false
		}
	}

	yield _send(this.url, param);
}

DingDing.prototype.sendActionCard = function* (data) {
	validate(data, {
		title: String,
		text: String
	});

	let param = {
		msgtype: 'actionCard',
		actionCard: {
			title: data.title,
			text: data.text,
			btnOrientation: data.btnOrientation || '',
			hideAvatar: data.hideAvatar || ''
		},
	};

	if(!data.singleTitle && !data.btns) {
		let msg = 'ActionCard跳转类型参数缺失';
		util.log(104, `钉钉消息发送失败, ${msg}`);
		radon.throw(10000500, [msg]);
	}

	let type = data.singleTitle ? 'whole' : 'independent';
	switch(type) {
		case 'whole':  		// 整体跳转类型
			validate(data, {
				singleTitle: String,
				singleURL: String
			});
			param.actionCard.singleTitle = data.singleTitle;
			param.actionCard.singleURL = data.singleURL;
			break;
		case 'independent':	// 独立跳转类型
			validate(data, {
				btns: Array
			});
			param.actionCard.btns = data.btns;
			break;
	}

	yield _send(this.url, param);
}

DingDing.prototype.sendFeedCard = function* (data) {
	validate(data, {
		links: Array
	});

	data.links.forEach(function(item) {
		validate(item, {
			title: String,
			messageURL: String,
			picURL: String
		});
	});

	let param = {
		msgtype: 'feedCard',
		feedCard: {
			links: data.links
		}
	}

	yield _send(this.url, param);
}

function* _send(remote, param) {
	if(!remote) {
		let err = '钉钉模块尚未设置发送消息的Webhook地址';
		util.log(104, `钉钉消息发送失败, ${err}`);
		radon.throw(10000500, [err]);
	}

	try {
		let result = yield http_request({
			remote: remote,
			method: 'POST',
			param: param
		});

		if(result && result.errcode) {
			util.log(104, `钉钉消息发送失败, errcode:${result.errcode} errmsg:${result.errmsg}`);
			radon.throw(10000500, [result.errmsg]);
		}
	}
	catch(e) {
		util.log(104, e.message);
		radon.throw(10000500, [e.message]);
	}
}

module.exports = DingDing;